    //
//  CZVistorView.swift
//  CZWeibo007
//
//  Created by Vize on 16/2/17.
//  Copyright © 2016年 Vize. All rights reserved.
//

import UIKit
    
//注册和登录点击时间的协议
protocol CZVistorViewDelegate: NSObjectProtocol {
        //注册按钮被点击
        func visitorViewRegisterClick()
        
        //登录按钮被点击
        func visitorViewLoginClick()
    }
    

class CZVistorView: UIView {
    
    //代理，用weak修饰
    weak var delegate: CZVistorViewDelegate?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        prepareUI()
    }
    
    //转轮动画
    func startRotationAnimation() {
        
        //用核心动画，转动
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        //设置参数
        anim.toValue = 2 * M_PI /// 角度
        anim.repeatCount = MAXFLOAT  ///重复次数
        anim.duration = 20 ///时间
        
        //动画完成时不移除
        anim.removedOnCompletion = false
        
        //给图层添加核心动画
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    //暂停旋转
    func pauseAnimation() {
        
        //记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        //设置动画速度为0
        iconView.layer.speed = 0
        
        //设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    //恢复旋转
    func resumeAnimation() {
        
        //获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        //设置动画速度为1
        iconView.layer.speed = 1
        //重置偏移时间
        iconView.layer.timeOffset = 0
        //重置开始时间
        iconView.layer.beginTime = 0
        //计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        //设置开始时间
        iconView.layer.beginTime = timeSincePause
    }
    
    func setVistorInfo(imageName:String, message:String) {
        //替换转轮的图片
        iconView.image = UIImage(named: imageName)
        
        //隐藏房子
        honmeView.hidden = true
        
        //遮罩视图放到底部
//        self.sendSubviewToBack(coverView)
        coverView.hidden = true
        //消息内容
        messageLabel.text = message
    }

    private func prepareUI() {
        //添加控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(honmeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        honmeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        
        //设置位置
        //轮子
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //房子
        self.addConstraint(NSLayoutConstraint(item: honmeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: honmeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //消息
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
//        //注册
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
//        //登录
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        //遮罩视图
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: loginButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }

    //MARK: - 懒加载
    //转轮
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //房子
    private lazy var honmeView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //消息
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.text = "关注一些人，看看这里有神马惊喜！"
        label.numberOfLines = 0
        return label
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        //添加点击事件
        button.addTarget(self, action: "ragisterClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "loginCilck", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // MARK: - 按钮点击事件
    //用private修饰点击方法，系统会找不到，需要加@objc
    @objc private func loginCilck() {
        
        
        
        delegate?.visitorViewLoginClick()
        
    }

    
    @objc private func ragisterClick() {
        
        delegate?.visitorViewRegisterClick()
        
    }
    

}

    